package com.laex;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class PieChartManager extends Canvas {
	int[] data;
	String[] str = {""};
	       
	int colors[] = { 0xFF0000, 0xA9E969, 0x00FFFF, 0xC675EC, 0x008800, 0x00C400 };

	public PieChartManager() {
		this.data = new int[]{0, 0, 0, 0, 0};
	}
	
	public void drawPieChart(int[] data, String[] str) {
		this.data = data;
		this.str = str;
	}

	public void paint(Graphics g) {
		int width = this.getWidth() - 20;
		int height = this.getHeight() - 20;

		g.setColor(255, 255, 255);
		g.fillRect(0, 0, width, height);

		int sum = 0;
		for (int i = 0; i < data.length; i++) {
			sum += data[i];
		}
		int deltaAngle = 360 * 100 / sum / 100;
		int x = 30;
		int y = 10;
		int diameter;

		if (width > height)
			diameter = height - y * 2;
		else
			diameter = width - x * 2;

		int startAngle = 0;
		
		int strX = 4;
		int strY = 0;
		int cati = 0;

		for (int i = 0; i < data.length; i++) {
			g.setColor(colors[i]);
			g.fillArc(x, y, diameter, diameter, startAngle, deltaAngle
					* data[i]);
			startAngle += deltaAngle * data[i];
			
			if (cati <= str.length - 1) {
				g.drawString(str[cati], strX, strY, Graphics.TOP|Graphics.LEFT);
				strY += 15;
				cati++;
			}
		}
	}
}
